package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrOptional;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Optional;

public class READRETOMDDATA extends AxdrType {
    private byte[] dataCode = null;
    private OMD omd = null;
    private AxdrEnum dar = null;
    private AxdrOptional<Data> retData = new AxdrOptional<>(new Data(), false);
    private OAD oad = null;
    private GetResult result = null;

    public READRETOMDDATA() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public OMD getOmd() {
        return omd;
    }

    public AxdrEnum getDar() {
        return dar;
    }

    public AxdrOptional<Data> getRetData() {
        return retData;
    }

    public OAD getOad() {
        return oad;
    }

    public GetResult getResult() {
        return result;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(OMD omd, AxdrEnum dar, Data retData, OAD oad, GetResult result) {
        this.omd = omd;
        this.dar = dar;
        this.retData = new AxdrOptional<>(new Data(), false);
        this.oad = oad;
        this.result = result;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.omd = new OMD();
        codeLength += this.omd.decode(input);
        this.dar = new AxdrEnum();
        this.dar.setConst();
        codeLength += this.dar.decode(input);
        this.retData = new AxdrOptional<>(new Data(), false);
        codeLength += this.retData.decode(input);
        this.oad = new OAD();
        codeLength += this.oad.decode(input);
        this.result = new GetResult();
        codeLength += this.result.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.oad != null && this.dar != null && this.omd != null && this.retData != null && this.result != null) {
                codeLength += this.result.encode(output);
                codeLength += this.oad.encode(output);
                codeLength += this.retData.encode(output);
                codeLength += this.dar.encode(output);
                codeLength += this.omd.encode(output);
            } else {
                throw new IllegalStateException("omd || dar || retData || oad || result is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.omd != null && this.dar != null && this.retData != null && this.oad != null && this.result != null) {
            return "sequence: {" + "omd: " + this.omd + ", dar: " + this.dar + ", retdata: " + this.retData + ", oad: " + this.oad + ", result: " + this.result + "}";
        } else {
            return "omd || dar || retData || oad || result is null";
        }
    }
}



